package http

import "github.com/gin-gonic/gin"

func InitHealthCheck(enging *gin.Engine, cfg *Health) {
	checkCfg := mergeHealthCfg(cfg)
	enging.GET(checkCfg.Path, func(ctx *gin.Context) {
		ctx.Abort()
		ctx.String(checkCfg.ResponseCode, checkCfg.Response)
	})
}

func mergeHealthCfg(input *Health) *Health {
	origin := &Health{
		Enable:       true,
		Path:         "/ping",
		Response:     "pong",
		ResponseCode: 200,
	}

	if !input.Enable {
		origin.Enable = input.Enable
	}

	if input.Path != "" {
		origin.Path = input.Path
	}

	if input.Response != "" {
		origin.Path = input.Response
	}

	if input.ResponseCode != 0 {
		origin.ResponseCode = input.ResponseCode
	}
	return origin
}
